package com.itmg.analyze.jp.data.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.itmg.analyze.jp.bean.AnalyzeBean;
import com.itmg.analyze.jp.data.IDataAnalyze;
import com.itmg.util.StringUtil;
import com.itmg.vo.SearchResultItemVO;
import com.itmg.vo.SearchResultVO;
import com.itmg.vo.TargetDataVO;

/**
 *  dom解析kakaku
 * @author xuehaipeng
 * @version 1.0
 * */

public class KakakuDataAnlyzeImpl implements IDataAnalyze {
	
	protected Logger log = Logger.getLogger(KakakuDataAnlyzeImpl.class.getName());
	
	public KakakuDataAnlyzeImpl() {
	}
	
	public SearchResultVO getSearchResultItemVO(TargetDataVO targetDataVO) throws Exception {	
		Document theDocument = AnalyzeBean.parseDocument(targetDataVO.getUrl());
		String sourceSiteName = targetDataVO.getSourceSiteName();
		SearchResultVO searchResultVO = new SearchResultVO();
		if(theDocument == null)
			return null;
		Element theRootElement = theDocument.getDocumentElement();
		NodeList theFirstNodeList = theRootElement.getChildNodes();
		List<SearchResultItemVO> searchResultItemVOList = new ArrayList<SearchResultItemVO>();
		for(int i=0;i < theFirstNodeList.getLength();i++)
		{
			Node machingNode = theFirstNodeList.item(i);
			if(machingNode.getNodeName().equals("NumOfResult"))
			{
				String NumOfResult = machingNode.getTextContent().trim();
				if(!"".equals(NumOfResult))
				{
					searchResultVO.setTotalResultNumber(Integer.valueOf(NumOfResult));
				}			
			}
			if(machingNode.getNodeName().equals("Item"))
			{
				SearchResultItemVO searchResultItemVO = new SearchResultItemVO();
				NodeList nodeCrawlerList = machingNode.getChildNodes();
				for(int j=0;j<nodeCrawlerList.getLength();j++)
				{				
					Node itemNode = nodeCrawlerList.item(j);
					if(itemNode.getNodeName().equals("ProductID"))
					{
						String ProductID = itemNode.getTextContent().trim();
						if(!"".equals(ProductID))
						{
							searchResultItemVO.setProductID(ProductID);
						}				
					}
					if(itemNode.getNodeName().equals("ProductName"))
					{
						String ProductName = itemNode.getTextContent().trim();
						if(!"".equals(ProductName))
						{
							searchResultItemVO.setProductName(ProductName);
						}
					}
					if(itemNode.getNodeName().equals("Comment"))
					{
						String comment = itemNode.getTextContent().trim();
						comment = StringUtil.replaceHtmlCode(comment);
						if(!"".equals(comment))
						{
							searchResultItemVO.setComment(comment);
						}
					}
					if(itemNode.getNodeName().equals("MakerName"))
					{
						String MakerName = itemNode.getTextContent().trim();
						if(!"".equals(MakerName))
						{
							searchResultItemVO.setMakerName(MakerName);
						}
					}
					if(itemNode.getNodeName().equals("ItemPageUrl"))
					{
						String productUrl = itemNode.getTextContent().trim();
						if(!"".equals(productUrl))
						{
							searchResultItemVO.setProductUrl(productUrl);
						}
					}
					if(itemNode.getNodeName().equals("ImageUrl"))
					{
						String smallImageUrl = itemNode.getTextContent().trim();
						if(!"".equals(smallImageUrl))
						{
							searchResultItemVO.setSmallImageUrl(smallImageUrl);
						}
					}
					if(itemNode.getNodeName().equals("LowestPrice"))
					{
						String LowestPrice = itemNode.getTextContent().trim();
						if(!"".equals(LowestPrice))
						{
							searchResultItemVO.setLowestPrice(Integer.parseInt(LowestPrice));
						}
					}
					if(itemNode.getNodeName().equals("PvRanking"))
					{
						String PvRanking = itemNode.getTextContent().trim();
						if(!"".equals(PvRanking))
						{
							searchResultItemVO.setPvRanking(Integer.parseInt(PvRanking));
						}
					}
					if(itemNode.getNodeName().equals("TotalScoreAve"))
					{
						String evaluation = itemNode.getTextContent().trim();
						if(!"".equals(evaluation))
						{
							searchResultItemVO.setEvaluation(Double.parseDouble(evaluation));
						}
					}
					if(itemNode.getNodeName().equals("SaleDate"))
					{
						String SaleDate = itemNode.getTextContent().trim();
						if(!"".equals(SaleDate))
						{
							searchResultItemVO.setSaleStartTime(SaleDate);
						}
					}
				}
				String md5 = searchResultItemVO.getMyProductMd5();
				searchResultItemVO.setProductMd5(md5);
				searchResultItemVO.setSourceSiteName(sourceSiteName);
				searchResultItemVOList.add(searchResultItemVO);
			}
		}
		searchResultVO.setResultItemList(searchResultItemVOList);
		return searchResultVO; 
	}
}
